package cn.argentoaskia.crm.service.update.impl;

import cn.argentoaskia.crm.dao.EmployeeDAO;
import cn.argentoaskia.crm.service.delete.EmployeeDeleteService;
import cn.argentoaskia.crm.service.update.EmployeeUpdateService;
import cn.argentoaskia.crm.utils.ValueEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class EmployeeUpdateServiceImpl implements EmployeeUpdateService {

    @Autowired
    private EmployeeDAO employeeDAO;


    @Override
    public boolean update(Integer id, String field, String value) {
        System.out.println("当前修改的字段名："  + field);
        if (field.equalsIgnoreCase("workstate")){
            // 用来格式化日期。
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            // 新员工入职
            if (value.equalsIgnoreCase("在职")){

                Date date = new Date();
                String format = simpleDateFormat.format(date);
                employeeDAO.update(id, "beginDate", format);
            }
            // 员工离职
            else{
                Date date = new Date();
                String format = simpleDateFormat.format(date);
                employeeDAO.update(id, "notWorkDate", format);
            }
        }

        int update = 0;
        if ("在职".equals (value)) {
            update = employeeDAO.update(id, "workState", value);
        }

        if ("离职".equals (value)) {

            update = employeeDAO.update(id, "workState", value);
        }

        return update > 0;
    }
}
